![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
The bach npm package is a tool for composing asynchronous functions in series or parallel. It is particularly useful for orchestrating tasks that need to be executed in a specific order or concurrently, often seen in build processes or complex workflows.
Series Execution
Executes tasks one after another. Each task will start only after the previous one has completed.
const { series } = require('bach');
function task1(cb) { console.log('Task 1'); cb(); }
function task2(cb) { console.log('Task 2'); cb(); }
const tasks = series(task1, task2);
tasks(function(err) { console.log('Done executing tasks in series.'); });
Parallel Execution
Executes tasks simultaneously. All tasks will start at the same time and run concurrently.
const { parallel } = require('bach');
function task1(cb) { console.log('Task 1'); cb(); }
function task2(cb) { console.log('Task 2'); cb(); }
const tasks = parallel(task1, task2);
tasks(function(err) { console.log('Done executing tasks in parallel.'); });
Settled Parallel Execution
Executes tasks in parallel and collects their results. Even if one task fails, the others will continue to execute.
const { parallel } = require('bach');
function task1(cb) { console.log('Task 1'); cb(null, 'result1'); }
function task2(cb) { console.log('Task 2'); cb(null, 'result2'); }
const tasks = parallel(task1, task2);
tasks(function(err, results) { console.log('Results:', results); });
The 'async' package provides a powerful collection of functions for working with asynchronous JavaScript. It offers more utilities than bach, such as map, filter, and reduce for collections, control flow functions, and utilities for working with functions. It is more feature-rich and can be considered heavier than bach.
The 'run-parallel' package is a simple module that runs an array of functions in parallel, but without any of the additional control flow features or utilities that 'async' provides. It is more focused and lightweight, similar to bach's parallel execution feature.
The 'run-series' package is similar to 'run-parallel' but for running tasks in series instead of parallel. It is a minimalistic tool that does one thing well, akin to bach's series execution feature.
Compose your async functions with elegance
With Bach, it is very easy to compose async functions to run in series or parallel.
var bach = require('bach');
function fn1(cb){
cb(null, 1);
}
function fn2(cb){
cb(null, 2);
}
function fn3(cb){
cb(null, 3);
}
var seriesFn = bach.series(fn1, fn2, fn3);
// fn1, fn2, and fn3 will be run in series
seriesFn(function(err, res){
if(err){ // in this example, err is undefined
// handle error
}
// handle results
// in this example, res is [1, 2, 3]
});
var parallelFn = bach.parallel(fn1, fn2, fn3);
// fn1, fn2, and fn3 will be run in parallel
parallelFn(function(err, res){
if(err){ // in this example, err is undefined
// handle error
}
// handle results
// in this example, res is [1, 2, 3]
});
Since the composer functions just return a function that can be called, you can combine them.
var combinedFn = bach.series(fn1, bach.parallel(fn2, fn3));
// fn1 will be executed before fn2 and fn3 are run in parallel
combinedFn(function(err, res){
if(err){ // in this example, err is undefined
// handle error
}
// handle results
// in this example, res is [1, [2, 3]]
});
Functions are called with async-done, so you can return a stream or promise. The function will complete when the stream ends/closes/errors or the promise fulfills/rejects.
// streams
var fs = require('fs');
function streamFn1(){
return fs.createReadStream('./example')
.pipe(fs.createWriteStream('./example'));
}
function streamFn2(){
return fs.createReadStream('./example')
.pipe(fs.createWriteStream('./example'));
}
var parallelStreams = bach.parallel(streamFn1, streamFn2);
parallelStreams(function(err){
if(err){ // in this example, err is undefined
// handle error
}
// all streams have emitted an 'end' or 'close' event
});
// promises
var when = require('when');
function promiseFn1(){
return when.resolve(1);
}
function promiseFn2(){
return when.resolve(2);
}
var parallelPromises = bach.parallel(promiseFn1, promiseFn2);
parallelPromises(function(err, res){
if(err){ // in this example, err is undefined
// handle error
}
// handle results
// in this example, res is [1, 2]
});
All errors are caught in a domain and passed to the final callback as the first argument.
function success(cb){
setTimeout(function(){
cb(null, 1);
}, 500);
}
function error(){
throw new Error('Thrown Error');
}
var errorThrownFn = bach.parallel(error, success);
errorThrownFn(function(err, res){
if(err){
// handle error
// in this example, err is an error caught by the domain
}
// handle results
// in this example, res is [undefined]
});
Something that may be encountered when an error happens in a parallel composition is the callback
will be called as soon as the error happens. If you want to continue on error and wait until all
functions have finished before calling the callback, use settleSeries
or settleParallel
.
function success(cb){
setTimeout(function(){
cb(null, 1);
}, 500);
}
function error(cb){
cb(new Error('Async Error'));
}
var parallelSettlingFn = bach.settleParallel(success, error);
parallelSettlingFn(function(err, res){
// all functions have finished executing
if(err){
// handle error
// in this example, err is an error passed to the callback
}
// handle results
// in this example, res is [1]
});
All bach APIs return an invoker function that takes a single callback as its only parameter.
The function signature is function(error, results)
.
Each method can optionally be passed an object of extension point functions as the last argument.
series(fns..., [extensions])
=> FunctionAll functions (fns
) passed to this function will be called in series when the returned function is
called. If an error occurs, execution will stop and the error will be passed to the callback function
as the first parameter.
The error parameter will always be a single error.
parallel(fns..., [extensions])
=> FunctionAll functions (fns
) passed to this function will be called in parallel when the returned
function is called. If an error occurs, the error will be passed to the callback function
as the first parameter. Any async functions that have not completed, will still complete,
but their results will not be available.
The error parameter will always be a single error.
settleSeries(fns..., [extensions])
=> FunctionAll functions (fns
) passed to this function will be called in series when the returned function is
called. All functions will always be called and the callback will receive all settled errors and results.
The error parameter will always be an array of errors.
settleParallel(fns..., [extensions])
=> FunctionAll functions (fns
) passed to this function will be called in parallel when the returned function is
called. All functions will always be called and the callback will receive all settled errors and results.
The error parameter will always be an array of errors.
An extension point object can contain:
create(fn, key)
=> storage
objectCalled before the async function or extension points are called. Receives the function and key to be executed in the future. The return value should be any object and will be passed to the other extension point methods. The storage object can keep any information needed between extension points and can be mutated within extension points.
before(storage)
Called before the async function is executed. Receives the storage object returned from the create
extension point.
after(storage)
Called after the async function is executed and completes successfully. Receives the storage object
returned from the create
extension point.
error(storage)
Called after the async function is executed and errors. Receives the storage object returned from
the create
extension point.
FAQs
Compose your async functions with elegance.
The npm package bach receives a total of 0 weekly downloads. As such, bach popularity was classified as not popular.
We found that bach demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.